package com.iot.web.form;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.witsight.platform.model.BoEntity;
import com.witsight.platform.model.IBaseObject;
import com.witsight.platform.util.lang.CollectionUtil;
/**   
 * 说明：各个bean之间的转换
 * @Title: BeanConverter.java 
 * @Package com.witsight.web.util 
 * @See: {@link BeanConverter}
 * Copyright: Copyright (c) 2017
 * Company:sany huax witsight team by product
 * @author: penght  
 * @date: 2017年10月10日 下午3:18:40 
 * @version: V1.0
 *
 */
public class BeanConverter {
	/**
	 * Logger
	 */
	private static final Logger log = LoggerFactory.getLogger(BeanConverter.class);
	// 需要转换的formBean的过滤子段
	private static final List<String> form_filters = CollectionUtil.arrayToList("class", "model", "pagination");
	/**
	 * 
	 * 说明：将vo转换为BoEntity
	 * @Title: toBoEntity
	 * @See: 
	 * @author penght 
	 * @param baseForm vo
	 * @return BoEntity
	 * @date: Jun 18, 2017 10:28:33 AM
	 */
	public static <F extends IBaseObject , E extends IBaseObject> BoEntity toBoEntity(
			BaseForm<F> baseForm) {
		BoEntity bo = new BoEntity();
		if (baseForm == null) {
			return bo;
		}
		try {
			// 获取实体对象
			BeanInfo formInfo = Introspector.getBeanInfo(baseForm.getClass());
			// 获取实体属性数组
			PropertyDescriptor[] propertyDescs =  formInfo.getPropertyDescriptors();
			for (PropertyDescriptor propertyDesc :  propertyDescs) {
				if (form_filters.contains(propertyDesc.getName())) {
					continue;
				}
				
				Method readMethod = propertyDesc.getReadMethod();
				Object result = readMethod.invoke(baseForm, new Object[0]);
				bo.set(propertyDesc.getName(), result);
			}
		} catch (Exception e) {
			if (log.isDebugEnabled()) {
				log.debug("form converter BoEntity fail.", e);
			}
			
			return bo;
		}
		return bo;
	}
}
